﻿$('#playerName').append(playerName);
$('#tableName').append(tableName);

function poller()
{
	jQuery.post(
		"poll",
		{ 'playerName': playerName, 'tableName': tableName },
		function (data)
		{
			setShit(data);
		}
	);
}

function setShit(data)
{
	var dumpString;

	$('#leTable').empty();
	$('#leTable').append('<tr><th>Player Name</th><th>Player Status</th><th>Chu?</th></tr>');

	info = JSON.parse(data);

	for (var item in info)
	{
		dumpString = "";

		dumpString += "<tr";

		if (info[item]['playerStatus'] == "1")
		{
			dumpString += ' class="warning"';
		}
		else if (info[item]['playerStatus'] == "2")
		{
			dumpString += ' class="success"';
		}

		dumpString += '>';

		dumpString += '<td>' + info[item]['playerName'] + '</td><td>';

		if (info[item]['playerStatus'] == "1")
		{
			dumpString += 'Waiting to roll...'
		}
		else if (info[item]['playerStatus'] == "2")
		{
				dumpString += 'Rolled!';
		}

		dumpString += '</td>';

		dumpString += '<td';

		if (info[item]['playerResult'] == '0')
		{
			dumpString += ' id="chu" class="danger text-center"><span class="glyphicon glyphicon-hand-left"></span>';
		}
		else
		{
			dumpString += '>';
		}

		dumpString += "</td></tr>"

		$('#leTable').append(dumpString);
		
		// animateHand();
	}

	for (var item in info)
	{
		if (info[item]['playerName'] == playerName)
		{
			if (info[item]['playerStatus'] == 1)
			{
				$('#rollButton').removeAttr("disabled");
			}
			else
			{
				$('#rollButton').attr("disabled", "disabled")
			}
		}
	}
}

setInterval(poller, 3000);

$('#rollButton').click(function () {

	jQuery.post(
		"roll",
		{ 'playerName': playerName, 'tableName': tableName }
	);

	$('#rollButton').attr("disabled", "disabled")
})

function animateHand()
{
	$('#chu').fadeOut(function () {
		$('#chu').fadeIn();
	});
}